package com.nobody.facepk.persist.type;


import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;


public class IntegerList implements org.hibernate.usertype.UserType,Serializable{
	private static final int[] TYPES = new int[] { Types.CHAR };
	
	public int[] sqlTypes() {
		return TYPES;
	}

	public Class returnedClass() {
		return List.class;
	}

	public boolean equals(Object x, Object y) throws HibernateException {
		return ( x == y ) || ( x != null && x.equals( y ) );
//		if (x == y) return true;  
//        if (x != null && y != null) {  
//            List xList = (List) x;  
//            List yList = (List) y;  
//            if (xList.size() != yList.size())  
//                return false;  
//            for (int i = 0; i < xList.size(); i++) {  
//                if (!xList.get(i).equals(yList.get(i)))  
//                    return false;  
//            }  
//            System.out.println("returned true");
//            return true;  
//        }  
//        System.out.println("returned false");
//        return false;
	}

	public int hashCode(Object x) throws HibernateException {
		return x.hashCode();
	}

	public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
//		String value = (String) Hibernate.STRING.nullSafeGet(rs, names[0]);
		String value = rs.getString(names[0]);
		return TypeUtils.stringToList(Integer.class, value);
	}

	public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
		if ( value != null ) {
			List<Integer> list = (List<Integer>) value;
			st.setString(index,TypeUtils.listToString(list));
		}
		else {
			st.setNull( index, sqlTypes()[0] );
		}
	}

	public Object deepCopy(Object value) throws HibernateException {
		return new ArrayList((List)value);
	}

	public boolean isMutable() {
		return false;
	}

	public Serializable disassemble(Object value) throws HibernateException {
		return (Serializable) value;
	}

	public Object assemble(Serializable cached, Object owner) throws HibernateException {
		return cached;
	}

	public Object replace(Object original, Object target, Object owner) throws HibernateException {
		return original;
	}
}
